# import os.path
# import re
# import requests
#
# """
# 使用搜索找内容，再找规律
# """
#
#
# # 爬取第4页主播的以标题命名头像的image
# def main():
#     # 过滤文件名中的非法字符
#     def sanitize_filename(filename):
#         # 替换Windows系统中不允许的特殊字符
#         invalid_chars = r'[\\/*?:"<>|]'
#         return re.sub(invalid_chars, '_', filename)
#
#     url = "https://www.douyu.com/gapi/rknc/directory/mixListV1/0_0/4?readList=12293190%2C1196914%2C656750%2C9724804%2C593061%2C6841192%2C12821%2C71415%2C9999%2C36252%2C6503272%2C178432%2C1457640%2C312212%2C288016%2C1667826%2C10568722%2C24422"
#     header = {
#         "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36"
#     }
#     res = requests.get(url, headers=header)
#     result = res.json()
#
#     # 创建保存目录（如果不存在）
#     save_dir = "斗鱼直播"
#     if not os.path.exists(save_dir):
#         os.mkdir(save_dir)
#
#     for r in result['data']['rl']:
#         title = r['rn']
#         # 处理标题，去除非法字符
#         safe_title = sanitize_filename(title)
#         print(f"正在下载 {title} -> 保存为 {safe_title}.jpg")
#
#         image = r['av']
#         try:
#             res_image = requests.get(image)
#             # 保存图片
#             with open(f'./{save_dir}/{safe_title}.jpg', 'wb') as f:
#                 f.write(res_image.content)
#         except Exception as e:
#             print(f"下载 {title} 失败: {str(e)}")
#
#
# if __name__ == '__main__':
#     main()

import os.path
import re
import requests

"""
使用搜索找内容，再找规律
"""


# 爬取100页主播的以标题命名头像的image
def main():
    for i in range(1,101):
        print(f"正在下载第{i}页.............")
        # 过滤文件名中的非法字符
        def sanitize_filename(filename):
            # 替换Windows系统中不允许的特殊字符
            invalid_chars = r'[\\/*?:"<>|]'
            return re.sub(invalid_chars, '_', filename)

        url = f"https://www.douyu.com/gapi/rknc/directory/mixListV1/0_0/{i}?readList=12293190%2C1196914%2C656750%2C9724804%2C593061%2C6841192%2C12821%2C71415%2C9999%2C36252%2C6503272%2C178432%2C1457640%2C312212%2C288016%2C1667826%2C10568722%2C24422"
        header = {
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36"
        }

        res = requests.get(url, headers=header)
        result = res.json()

        # 创建保存目录（如果不存在）
        save_dir = "斗鱼直播"
        if not os.path.exists(save_dir):
            os.mkdir(save_dir)

        for r in result['data']['rl']:
            title = r['rn']
            # 处理标题，去除非法字符
            safe_title = sanitize_filename(title)
            print(f"正在下载 {title} -> 保存为 {safe_title}.jpg")

            image = r['av']
            try:
                res_image = requests.get(image)
                # 保存图片
                with open(f'./{save_dir}/{safe_title}.jpg', 'wb') as f:
                    f.write(res_image.content)
            except Exception as e:
                print(f"下载 {title} 失败: {str(e)}")


if __name__ == '__main__':
    main()
